package pri.zxx.excelgenerate.dataentity.tabledata.growth;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import pri.zxx.excelgenerate.dataentity.entity.CreatRequest;
import pri.zxx.excelgenerate.dataentity.entity.ModuleInfoEnum;
import pri.zxx.excelgenerate.dataentity.entity.ModuleMetaInfo;
import pri.zxx.excelgenerate.dataentity.tabledata.MultiTitleModuleCreatorTemplate;
import pri.zxx.excelgenerate.dataentity.tabledata.RangeVo;
import pri.zxx.excelgenerate.dataentity.tabledata.TableDefinition;
import pri.zxx.excelgenerate.excelhanler.ExcelUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 各 阶 段 生 长 性能 模块
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Component
@Slf4j
public class GrowthRangeModuleCreator extends MultiTitleModuleCreatorTemplate {


    //todo 注入数据获取的bean

    @Override
    public List<ModuleMetaInfo> creator(CreatRequest creatRequest) {


        List<ModuleMetaInfo> modules = new ArrayList<>();

        CreatRequest.TableCreateReq tcr = new CreatRequest.TableCreateReq(4.0, 1, null);

        /* ***************************    表格数据      **************************/

        TableDefinition tableDefinition = new TableDefinition();
        List<RangeVo> unLoadData = new ArrayList<>();

        switch (creatRequest.getPredictionTypeEnum()) {
            case STARTING_PIGS:
                log.warn("种类：【{} 】不存在【{}】模块", creatRequest.getPredictionTypeEnum().getValue(), moduleInfo().getName());
                return null;
            case GESTATING_SOWS:
                //todo 调用获取数据的service
                // 模拟数据
                GrowthRangeGestatingSows gs = new GrowthRangeGestatingSows();
                GrowthRangeGestatingSows gs2 = new GrowthRangeGestatingSows();
                GrowthRangeGestatingSows gs3 = new GrowthRangeGestatingSows();
                GrowthRangeGestatingSows gs4 = new GrowthRangeGestatingSows();
                gs.setInitialBwforaa(BigDecimal.valueOf(1)).setFinalBwforaa(BigDecimal.valueOf(30)).setInitialDayinput(1).setFinalDayinput(30);
                gs2.setInitialBwforaa(BigDecimal.valueOf(30)).setFinalBwforaa(BigDecimal.valueOf(50)).setInitialDayinput(30).setFinalDayinput(50);
                gs3.setInitialBwforaa(BigDecimal.valueOf(50)).setFinalBwforaa(BigDecimal.valueOf(60)).setInitialDayinput(50).setFinalDayinput(60);
                gs4.setInitialBwforaa(BigDecimal.valueOf(60)).setFinalBwforaa(BigDecimal.valueOf(80)).setInitialDayinput(60).setFinalDayinput(80);
                unLoadData.add(gs);
                unLoadData.add(gs2);
                unLoadData.add(gs3);

                tableDefinition.setDataClass(GrowthRangeGestatingSows.class);

                break;
            case LACTATING_SOWS:
                //todo 调用获取数据的service
                // 模拟数据
                GrowthRangeLactatingSows ls = new GrowthRangeLactatingSows();
                GrowthRangeLactatingSows ls2 = new GrowthRangeLactatingSows();
                GrowthRangeLactatingSows ls3 = new GrowthRangeLactatingSows();
                GrowthRangeLactatingSows ls4 = new GrowthRangeLactatingSows();
                ls.setInitialBwforaa(BigDecimal.valueOf(1)).setFinalBwforaa(BigDecimal.valueOf(30)).setInitialDayinput(1).setFinalDayinput(30);
                ls2.setInitialBwforaa(BigDecimal.valueOf(30)).setFinalBwforaa(BigDecimal.valueOf(50)).setInitialDayinput(30).setFinalDayinput(50);
                ls3.setInitialBwforaa(BigDecimal.valueOf(50)).setFinalBwforaa(BigDecimal.valueOf(60)).setInitialDayinput(50).setFinalDayinput(60);
                ls4.setInitialBwforaa(BigDecimal.valueOf(60)).setFinalBwforaa(BigDecimal.valueOf(80)).setInitialDayinput(60).setFinalDayinput(80);
                unLoadData.add(ls);
                unLoadData.add(ls2);
                unLoadData.add(ls3);

                tableDefinition.setDataClass(GrowthRangeLactatingSows.class);
                break;
            case GROWING_FINISHING:
                tcr.setMAX_REPEAT(2.0).setMULT(2).setTitleList(Arrays.asList("预估值", "观测值"));
                //todo 调用获取数据的service
                // 模拟数据
                GrowthRangeGrowingFinishing op = new GrowthRangeGrowingFinishing();
                GrowthRangeGrowingFinishing op2 = new GrowthRangeGrowingFinishing();
                GrowthRangeGrowingFinishing op3 = new GrowthRangeGrowingFinishing();
                GrowthRangeGrowingFinishing op4 = new GrowthRangeGrowingFinishing();
                op.setInitialBwforaa(BigDecimal.valueOf(1)).setFinalBwforaa(BigDecimal.valueOf(30)).setInitialDayinput(1).setFinalDayinput(30);
                op2.setInitialBwforaa(BigDecimal.valueOf(30)).setFinalBwforaa(BigDecimal.valueOf(50)).setInitialDayinput(30).setFinalDayinput(50);
                op3.setInitialBwforaa(BigDecimal.valueOf(50)).setFinalBwforaa(BigDecimal.valueOf(60)).setInitialDayinput(50).setFinalDayinput(60);
                op4.setInitialBwforaa(BigDecimal.valueOf(60)).setFinalBwforaa(BigDecimal.valueOf(80)).setInitialDayinput(60).setFinalDayinput(80);
                unLoadData.add(op);
                unLoadData.add(op2);
                unLoadData.add(op3);

                tableDefinition.setDataClass(GrowthRangeGrowingFinishing.class);
                break;
            default:
                throw new RuntimeException("动物类型异常");

        }

        creatRequest.setTableCreateReq(tcr);

        /* ***************************    分页组装表格      **************************/
        List<CreatRequest.Range> ranges = creatRequest.getRanges();
        //有范围要求的表格 根据选择的范围拆分 向上取整
        int tableCount = (int) Math.ceil(ranges.size() / tcr.getMAX_REPEAT());//创建module数量

        for (int j = 0; j < tableCount; j++) {

            ModuleMetaInfo metaInfo = new ModuleMetaInfo();
            metaInfo.setModuleInfoEnum(moduleInfo());
            TableDefinition innerTd = new TableDefinition();
            BeanUtils.copyProperties(tableDefinition,innerTd);

            List<CreatRequest.Range> rg = ranges.stream().skip((long) (tcr.getMAX_REPEAT() * j)).limit((long) tcr.getMAX_REPEAT()).collect(Collectors.toList());
            List<RangeVo> pageData = unLoadData.stream().filter(opVitamin -> {
                //判断是体重范围 还是 时间范围
                Boolean isWeight = ExcelUtil.isWeight(creatRequest);
                String rangeStr;
                if (isWeight) {
                    BigDecimal finalBwforaa = opVitamin.getFinalBwforaa();
                    BigDecimal initialBwforaa = opVitamin.getInitialBwforaa();
                    rangeStr = initialBwforaa.toString() + "-" + finalBwforaa.toString();
                } else {
                    Integer initialDayinput = opVitamin.getInitialDayinput();
                    Integer finalDayinput = opVitamin.getFinalDayinput();
                    rangeStr = initialDayinput + "-" + finalDayinput;
                }
                return rg.stream().anyMatch(range -> rangeStr.equals(range.getStart() + "-" + range.getEnd()));
            }).collect(Collectors.toList());

            innerTd.setUnLoadData(pageData);

            CreatRequest cr = new CreatRequest();
            BeanUtils.copyProperties(creatRequest, cr);
            cr.setRanges(rg);
            //组装数据
            innerTd = dataHandle(moduleMetaInfoHandle(innerTd, cr), cr);

            metaInfo.setTableDefinition(innerTd);
            metaInfo.setModuleTitle(moduleInfo().getName() + JOIN_DECILITER + (j + 1));

            modules.add(metaInfo);
        }
        return modules;
    }

    @Override
    public ModuleInfoEnum moduleInfo() {
        return ModuleInfoEnum.OP_GROWTH_RANGE;
    }

}
